package com.yonyou.pmclouds.completedcatalog.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoInsertProcessor;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoUpdateProcessor;
import com.yonyou.pmclouds.basecom.bp.template.SingleDeleteBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleInsertBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleUpdateBpTemplate;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.exception.ConcurrentOperationException;
import com.yonyou.pmclouds.basecom.lock.AutoReleaseLockUtil;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.basecom.util.CommonFieldConst;
import com.yonyou.pmclouds.completedcatalog.entity.CompletedCatalogBVO;
import com.yonyou.pmclouds.completedcatalog.entity.CompletedCatalogVO;
import com.yonyou.pmclouds.completedcatalog.mapper.CompletedCatalogBVOMapper;
import com.yonyou.pmclouds.completedcatalog.mapper.CompletedCatalogVOMapper;
import com.yonyou.pmclouds.completedcatalog.processor.CompletedCatalogNameCheck;
import com.yonyou.pmclouds.completedcatalog.rmiitf.CompletedCatalogMaintain;
import com.yonyou.pmclouds.completedcollate.mapper.CompletedCollateBVOMapper;
import com.yonyou.pmclouds.completedcollate.mapper.CompletedCollateVOMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 竣工目录数据操作具体实现
 *
 * @author yzx
 */
@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = CompletedCatalogMaintain.class)
public class CompletedCatalogMaintainImpl implements CompletedCatalogMaintain {
    private static final String LOCK_PREFIX = "CompletedCatalog_";
    private static final String[] HEAD_UPDATE_FIELDS = new String[]{CommonFieldConst.SNAME, CompletedCatalogVO.CATALOG_TYPE};
    private static final String[] BODY_UPDATE_FIELDS = new String[]{CommonFieldConst.SNAME, CompletedCatalogBVO.DATUM_CONTENT,
            CompletedCatalogBVO.SEQUENCE, CompletedCatalogBVO.OSS_NAME, CompletedCatalogBVO.DATUM_TYPE};

    @Autowired
    private CompletedCatalogVOMapper completedCatalogVOMapper;

    @Autowired
    private CompletedCatalogBVOMapper completedCatalogBVOMapper;

    @Autowired
    private CompletedCollateVOMapper collateVOMapper;
    @Autowired
    private CompletedCollateBVOMapper collateBVOMapper;

    @Override
    public String insert(CompletedCatalogVO completedCatalogVO) throws BusinessException {
        //  add lock
        if (!AutoReleaseLockUtil.addAutoReleaseLock(LOCK_PREFIX + completedCatalogVO.getPkTenant())) {
            throw new ConcurrentOperationException();
        }
        SingleInsertBpTemplate<CompletedCatalogVO> bp = new SingleInsertBpTemplate<>(completedCatalogVOMapper);
        SingleInsertBpTemplate<CompletedCatalogBVO> detailBp = new SingleInsertBpTemplate<>(completedCatalogBVOMapper);
        // 添加审计信息
        bp.addBeforeProcessor(new AuditInfoInsertProcessor<CompletedCatalogVO>());
        // 目录名称重复性校验
        bp.addBeforeProcessor(new CompletedCatalogNameCheck(completedCatalogVOMapper));
        bp.insert(completedCatalogVO);
        // 保存完主表后保存表体内容
        if (completedCatalogVO.getCompletedCatalogBVOS() != null) {
            for (CompletedCatalogBVO catalogBVO : completedCatalogVO.getCompletedCatalogBVOS()) {
                catalogBVO.setPkCompletedCatalog(completedCatalogVO.getPkCompletedCatalog());
                // 添加审计信息
                detailBp.addBeforeProcessor(new AuditInfoInsertProcessor<CompletedCatalogBVO>());
                detailBp.insert(catalogBVO);
            }
        }
        return completedCatalogVO.getPkCompletedCatalog();
    }

    @Override
    public String update(CompletedCatalogVO completedCatalogVO) throws BusinessException {
        SingleUpdateBpTemplate<CompletedCatalogVO> bp = new SingleUpdateBpTemplate<>(completedCatalogVOMapper, HEAD_UPDATE_FIELDS);
        SingleInsertBpTemplate<CompletedCatalogBVO> detailInBp = new SingleInsertBpTemplate<>(completedCatalogBVOMapper);
        SingleUpdateBpTemplate<CompletedCatalogBVO> detailUpBp = new SingleUpdateBpTemplate<>(completedCatalogBVOMapper, BODY_UPDATE_FIELDS);
        //审计信息
        bp.addBeforeProcessor(new AuditInfoUpdateProcessor<CompletedCatalogVO>());
        // 目录名称重复性校验
        bp.addBeforeProcessor(new CompletedCatalogNameCheck(completedCatalogVOMapper));
        bp.update(completedCatalogVO);
        // 更新完主表后,更新或插入表体数据
        if (completedCatalogVO.getCompletedCatalogBVOS() != null) {
            for (CompletedCatalogBVO catalogBVO : completedCatalogVO.getCompletedCatalogBVOS()) {
                // 添加审计信息
                detailUpBp.addBeforeProcessor(new AuditInfoUpdateProcessor<CompletedCatalogBVO>());
                catalogBVO.setPkCompletedCatalog(completedCatalogVO.getPkCompletedCatalog());
                if (StringUtils.isEmpty(catalogBVO.getPkCompletedCatalogB())) {
                    detailInBp.insert(catalogBVO);
                } else {
                    detailUpBp.update(catalogBVO);
                }
            }
        }
        return completedCatalogVO.getPkCompletedCatalog();
    }

    @Override
    public void deleteCatalog(CompletedCatalogVO completedCatalogVO) throws BusinessException {
        //  add lock
        if (!AutoReleaseLockUtil.addAutoReleaseLock(LOCK_PREFIX + RuntimeEnvironment.getTenantId())) {
            throw new ConcurrentOperationException();
        }
        SingleDeleteBpTemplate<CompletedCatalogVO> bp = new SingleDeleteBpTemplate<>(completedCatalogVOMapper);
        // 删除前校验(被引用不能删除)(暂时屏蔽)
        //bp.addBeforeProcessor(new CompletedCatalogDelCheck(collateVOMapper));
        bp.delete(completedCatalogVO);
    }

    @Override
    public void deleteCatalogDetail(CompletedCatalogBVO catalogBVO) throws BusinessException {
        //  add lock
        if (!AutoReleaseLockUtil.addAutoReleaseLock(LOCK_PREFIX + RuntimeEnvironment.getTenantId())) {
            throw new ConcurrentOperationException();
        }
        SingleDeleteBpTemplate<CompletedCatalogBVO> bp = new SingleDeleteBpTemplate<>(completedCatalogBVOMapper);
        // 被引用不能删除校验(暂时屏蔽)
        //bp.addBeforeProcessor(new CompletedCatalogBodyDelCheck(collateBVOMapper));
        bp.delete(catalogBVO);
    }
}
